package swb.api.com.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import swb.api.com.repository.WithdrawRecordRepository;
import swb.api.com.repository.dto.WithdrawRecord;
import swb.api.com.service.WithdrawRecordService;

import java.time.LocalDateTime;
import java.util.Optional;

@Service
public class WithdrawRecordServiceImpl implements WithdrawRecordService {

    @Autowired
    private WithdrawRecordRepository withdrawRecordRepository;

    @Override
    public Page<WithdrawRecord> findByStatus(Integer status, Pageable pageable) {
        if (status != null) {
            return withdrawRecordRepository.findByStatus(status, pageable);
        }
        return withdrawRecordRepository.findAll(pageable);
    }

    @Override
    public void processWithdraw(Long id, Integer status, String remark) {
        Optional<WithdrawRecord> recordOptional = withdrawRecordRepository.findById(id);
        if (recordOptional.isPresent()) {
            WithdrawRecord record = recordOptional.get();
            record.setStatus(status);
            record.setRemark(remark);
            record.setUpdatedAt(LocalDateTime.now());
            withdrawRecordRepository.save(record);
        }
    }
}
